package src.mcpichess.control;

import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;

import pi.Minecraft;

public class GUI {

	private JFrame frmMcpiChessStartup;
	private JTextField textField;
	private JTextArea console;
	private JButton btnConnect;
	private JButton btnNewGame;
	public Minecraft mc;
	public Main main;
	boolean firstGame = true;
	private boolean isConnected = false;

	/**
	 * Create the application.
	 */
	public GUI() {
		initialize();
		//Main main = new Main();
	}
	
	/**
	 * Append the message log
	 */
	
	public void addMessage(String add){
		console.setText( console.getText() + add + "\n");
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmMcpiChessStartup = new JFrame();
		frmMcpiChessStartup.setResizable(false);
		frmMcpiChessStartup.setTitle("MCPI Chess Startup");
		frmMcpiChessStartup.setBounds(100, 100, 435, 272);
		frmMcpiChessStartup.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JLabel lblMcpiChess = new JLabel("MCPI Chess");
		lblMcpiChess.setBackground(Color.WHITE);
		lblMcpiChess.setFont(new Font("Tahoma", Font.PLAIN, 23));
		lblMcpiChess.setHorizontalAlignment(SwingConstants.CENTER);
		frmMcpiChessStartup.getContentPane().add(lblMcpiChess, BorderLayout.NORTH);
		
		JPanel panel = new JPanel();
		frmMcpiChessStartup.getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("IP Address:");
		lblNewLabel.setBounds(20, 35, 70, 17);
		lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 14));
		panel.add(lblNewLabel);
		
		textField = new JTextField();
		textField.setBounds(100, 35, 319, 20);
		panel.add(textField);
		textField.setText("192.168.2.7");
		textField.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("Enter the IP address of the Rasperry Pi you wish to connect to.");
		lblNewLabel_1.setBounds(20, 60, 399, 14);
		panel.add(lblNewLabel_1);
		
		JLabel lblAMinecraftPi = new JLabel("A Minecraft: Pi Edition project by Daniel Schon and Joey Freeland");
		lblAMinecraftPi.setBounds(20, 6, 399, 14);
		panel.add(lblAMinecraftPi);
		
		btnConnect = new JButton("Connect");
		btnConnect.setBounds(20, 85, 190, 23);
		panel.add(btnConnect);
		
		btnNewGame = new JButton("New Game");
		btnNewGame.setBounds(217, 85, 200, 23);
		panel.add(btnNewGame);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(20, 138, 399, 51);
		panel.add(scrollPane);
		
		console = new JTextArea();
		console.setLineWrap(true);
		scrollPane.setViewportView(console);
		console.setEditable(false);
		
		
		
		JLabel lblMessageConsole = new JLabel("Message Console:");
		lblMessageConsole.setBounds(20, 119, 190, 14);
		panel.add(lblMessageConsole);
		frmMcpiChessStartup.setVisible(true);
		listen();
	}
	
	/**
	 * Set the listeners
	 */
	 public void listen(){
		 
		 /*Listen for the press of the Connect Button*/
		 btnConnect.addActionListener( new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {

				/*Try to connect to the Pi*/
				String ip = textField.getText();
				try{
					mc = Minecraft.connect(ip);				//Connect to the pi
					addMessage("Successfully connected to " + ip);
					isConnected = true;
				}catch(Exception e){
					addMessage("Failed to connect to " + ip + "!");
				}
			}
			 
		 });
		 
		 /*Listen for the press of the New Game Button*/
		 btnNewGame.addActionListener( new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					if (isConnected){
						try{
							if (firstGame){
								main = new Main();
								main.start(mc);
								btnNewGame.setEnabled(false);
								btnConnect.setEnabled(false);
								addMessage("The game has begun!");
								firstGame = false;
							}else{
								main.restart();
								addMessage("The game has restarted!");
							}
								
						}catch(Exception e){
							e.printStackTrace();
							addMessage("Failed to create a game.");
						}
					}else{
						addMessage("You must first connect to a Pi running MC");
					}
				}
				 
			 });
	 }
}
